import pypvm
import sys
from signal import signal, SIGTERM
from time import sleep

# ============================ sig_h() =======================================
def sig_h(signum, traceback):
    print "SLAVE: got signal", signum
try: 

    signal(SIGTERM, sig_h)

# ============================ test parent() =================================
    parent = pypvm.parent()

# ============================ test probe() ==================================
    print "SLAVE: testing probe..."
    bufid = pypvm.probe(parent, 1)
    print "SLAVE: probe returned", bufid

# ============================ test bufinfo() ================================
    print "SLAVE: testing bufinfo..."
    if bufid:
        print "SLAVE: bufinfo returned", pypvm.bufinfo(bufid)
    else:
        print "SLAVE: no incoming messages"

# ============================ test recv() ===================================
    print "SLAVE: receiving data from tid %s..." % parent
    pypvm.recv(parent, 1)

# ============================ test up() =====================================
    print "SLAVE: unpacking data..."
    data = pypvm.upk()
    print "SLAVE: data received = %s" % data

# ============================ test mcast() ==================================
    print "SLAVE: receiving data from tid %s(mcast test)..." % parent
    pypvm.recv(parent, 2)

    #data = pypvm.upkbyte(3, 1)
    #print "SLAVE: data received = %s using upkbyte()" % data

    #data = pypvm.upkcplx(3, 1)
    #print "SLAVE: data received = %s using upkcplx" % data

    #data = pypvm.upkdcplx(3, 1)
    #print "SLAVE: data received = %s using upkdcplx" % data

    data = pypvm.upkdouble(3, 1)
    print "SLAVE: data received = %s using upkdouble" % data

    data = pypvm.upkfloat(3, 1)
    print "SLAVE: data received = %s using upkfloat" % data

    data = pypvm.upkint(3, 1)
    print "SLAVE: data received = %s using upkint" % data

    data = pypvm.upklong(3, 1)
    print "SLAVE: data received = %s using upklong" % data

    data = pypvm.upkshort(3, 1)
    print "SLAVE: data received = %s using upkshort" % data

# ============================ sleep ==========================================
    print "SLAVE: sleeping so I don't miss the signal"
    try:
        sleep(2)
    except IOError:
        # interrupted system call
        pass

# ============================ test exit() ===================================
    print "SLAVE: exiting pvm..."
    pypvm.exit()

except:
    print "A pypvm error occured!"
    print sys.exc_info()
